package com.skq.controller;

import com.skq.pojo.Category;
import com.skq.pojo.Result;
import com.skq.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author skq
 * @data 2024/6/7
 */
@Tag(name = "分类管理接口")
@RestController
@RequestMapping("/category")
@Validated
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "添加分类")
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        categoryService.add(category);
        return Result.success();

    }

    @Operation(summary = "查询分类列表")
    @GetMapping
    public Result<List<Category>> list() {
        List<Category> categories = categoryService.list();
        return Result.success(categories);
    }

    @Operation(summary = "根据id查询分类详情")
    @GetMapping("/detail/{id}")
    public Result<Category> detail(@PathVariable Integer id) {
        Category category = categoryService.findById(id);
        if (category == null) {
            return Result.error("分类不存在");
        }
        return Result.success(category);

    }

    @Operation(summary = "更新分类")
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    @Operation(summary = "删除分类")
    @DeleteMapping
    public Result delete(Integer id) {
        categoryService.delete(id);

        return Result.success();
    }


}
